import { FC, useState } from 'react';
import { Form, Input, Button, Card, message } from 'antd';
import { UserOutlined, LockOutlined, MobileOutlined } from '@ant-design/icons';
import { useNavigate } from 'react-router-dom';
import styles from './Register.module.css';

interface RegisterForm {
  username: string;
  phone: string;
  password: string;
  confirmPassword: string;
  verifyCode: string;
}

const Register: FC = () => {
  const navigate = useNavigate();
  const [form] = Form.useForm<RegisterForm>();
  const [loading, setLoading] = useState(false);
  const [countdown, setCountdown] = useState(0);

  // 获取验证码
  const handleGetVerifyCode = async () => {
    try {
      const phone = form.getFieldValue('phone');
      if (!phone) {
        message.error('请先输入手机号');
        return;
      }
      
      setLoading(true);
      // TODO: 调用发送验证码接口
      message.success('验证码已发送');
      setCountdown(60);
      
      // 倒计时
      const timer = setInterval(() => {
        setCountdown((prev) => {
          if (prev <= 1) {
            clearInterval(timer);
            return 0;
          }
          return prev - 1;
        });
      }, 1000);
    } catch (error) {
      message.error('验证码发送失败');
    } finally {
      setLoading(false);
    }
  };

  // 提交注册
  const onFinish = async (values: RegisterForm) => {
    try {
      setLoading(true);
      // TODO: 调用注册接口
      message.success('注册成功');
      navigate('/login');
    } catch (error) {
      message.error('注册失败');
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className={styles.container}>
      <Card title="用户注册" className={styles.registerCard}>
        <Form
          form={form}
          name="register"
          onFinish={onFinish}
          autoComplete="off"
        >
          <Form.Item
            name="username"
            rules={[
              { required: true, message: '请输入用户名' },
              { min: 2, max: 20, message: '用户名长度为2-20个字符' }
            ]}
          >
            <Input
              prefix={<UserOutlined />}
              placeholder="请输入用户名"
              size="large"
            />
          </Form.Item>

          <Form.Item
            name="phone"
            rules={[
              { required: true, message: '请输入手机号' },
              { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }
            ]}
          >
            <Input
              prefix={<MobileOutlined />}
              placeholder="请输入手机号"
              size="large"
            />
          </Form.Item>

          <Form.Item
            name="password"
            rules={[
              { required: true, message: '请输入密码' },
              { min: 6, max: 20, message: '密码长度为6-20个字符' },
              {
                pattern: /^(?=.*[a-zA-Z])(?=.*\d).+$/,
                message: '密码必须包含字母和数字'
              }
            ]}
          >
            <Input.Password
              prefix={<LockOutlined />}
              placeholder="请输入密码"
              size="large"
            />
          </Form.Item>

          <Form.Item
            name="confirmPassword"
            dependencies={['password']}
            rules={[
              { required: true, message: '请确认密码' },
              ({ getFieldValue }) => ({
                validator(_, value) {
                  if (!value || getFieldValue('password') === value) {
                    return Promise.resolve();
                  }
                  return Promise.reject(new Error('两次输入的密码不一致'));
                },
              }),
            ]}
          >
            <Input.Password
              prefix={<LockOutlined />}
              placeholder="请确认密码"
              size="large"
            />
          </Form.Item>

          <Form.Item
            name="verifyCode"
            rules={[
              { required: true, message: '请输入验证码' },
              { len: 4, message: '验证码为4位数字' }
            ]}
          >
            <div className={styles.verifyCodeRow}>
              <Input
                placeholder="请输入验证码"
                size="large"
                maxLength={4}
              />
              <Button
                type="primary"
                onClick={handleGetVerifyCode}
                disabled={countdown > 0}
                loading={loading}
              >
                {countdown > 0 ? `${countdown}秒后重试` : '获取验证码'}
              </Button>
            </div>
          </Form.Item>

          <Form.Item>
            <Button type="primary" htmlType="submit" block size="large" loading={loading}>
              注册
            </Button>
          </Form.Item>

          <Form.Item>
            <Button type="link" onClick={() => navigate('/login')} block>
              已有账号？立即登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
};

export default Register; 